﻿using System.Net;

namespace TestWebApi.HttpRequest
{
    public class SendHttp
    {
        public static string SendGetByHttp(string url , Dictionary<string , string> head) {
           HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(url);
            httpWebRequest.Method = "GET";
            if (head != null)
            {
                foreach (string datastr in head.Keys) {
                   httpWebRequest.Headers.Add(datastr , head[datastr]);
                }

            }
            HttpWebResponse response =  (HttpWebResponse) httpWebRequest.GetResponse();
            var data = "";
            //类似java try  
            try {
                using (StreamReader S = new StreamReader(response.GetResponseStream()))
                {
                    data = S.ReadToEnd();
                   // Console.WriteLine(data);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return null;
            }
            
            return data;
        }
    }
}
